<?php

use Biz\model\brandActivity\JDSigin2020;


/**
 * @deprecated
 * 京东2020双十一签到活动
 * Class JDSiginAction
 */
class JDSiginListAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_NOTHING;

    public function main()
    {
        $typeOne = [20201105, 20201107, 20201109];//0:未开始 1：未签到 2：已签到
        $typeTwo = [20201106, 20201108, 20201110];//3：连续两天签到 4：未完成连续两天签到 5：未签到
        $startDay = min($typeOne);
        $endDay = max($typeTwo);
        $currDay = date('Ymd');
        $days = array_merge($typeOne, $typeTwo);
        sort($days);
        foreach ($days as $val) {
            $list[$val] = [
                'day' => $val,
                'time' => strtotime($val),
                'status' => in_array($val, $typeTwo) ? 5 : 0,//未开始
                'type' => in_array($val, $typeTwo) ? 1 : 0//区分是第一天还是第二天
            ];
        }
        $where = [
            'uid' => $this->uid,
            'addtime[>=]' => date('Y-m-d', strtotime($startDay)),
            'addtime[<]' => date('Y-m-d', strtotime($endDay) + 86400),
        ];
        $siginArr = $this->uid ? JDSigin2020::selectByWhere($where) : [];
        $siginDays = [];
        foreach ($siginArr as $row) {
            $day = date('Ymd', strtotime($row['addtime']));
            $siginDays[] = $day;
        }
        foreach ($list as $day => $row) {
            if (in_array($day, $typeOne) && $currDay >= $day) {
                if (in_array($day, $siginDays)) {
                    $list[$day]['status'] = 2;//已签到
                } else if ($day != $currDay) {
                    $list[$day]['status'] = 1;//未签到
                }
            } else if (in_array($day, $typeTwo)) {
                $previousDay = date('Ymd', strtotime($day) - 86400);
                if ($currDay >= $day) {
                    if (!in_array($day, $siginDays) && !in_array($previousDay, $siginDays)) {
                        if ($currDay != $day) {
                            $list[$day]['status'] = 4;//未完成连续两天签到
                        }
                    } else if (in_array($day, $siginDays)) {
                        if (in_array($previousDay, $siginDays)) {
                            $list[$day]['status'] = 3;//完成连续两天签到
                        } else {
                            $list[$day]['status'] = 4;//未完成连续两天签到
                        }
                    } else if (in_array($previousDay, $siginDays)) {
                        if ($currDay != $day) {
                            $list[$day]['status'] = 4;//未完成连续两天签到
                        }
                    }
                }
            }
        }
        $this->setSuccess(array_values($list));
    }
}